package com.datasecops.scannerservices.db;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * DB公共类
 * @author: SunLingzhi
 * @time: 2022/8/15 10:26
 */
public abstract class DBHelper {

    //获取连接
    public abstract <T> T getConnection(String id);

    //关闭连接
    public static void closeConnection(Connection c){
        try {
            c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    };
    /**
     * 初始化数据源
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/12 18:08
     */
    public abstract void addDataSource(String ip, int port, String user, String password,String Instance);

     /**
      * 获取差异化配置
      * @Description
      * @author SunLingzhi
      * @time 2022/8/15 11:16
      * @param
      * @return
      */
    public static Properties getCommonProp(String user, String password,String driver,String url,String properties){
        Properties prop = new Properties();
        prop.put("driver", driver);
        prop.put("url", url);
        if (null != properties){
            prop.setProperty("connectionProperties", "useUnicode=true;characterEncoding=UTF8;useSSL=true;serverTimezone=Asia/Shanghai");
        }
        prop.setProperty("useCursorFetch", "true");//开启流
        prop.setProperty("username", user);
        if (null != password){
            prop.setProperty("password", password);
        }
        prop.setProperty("initialSize", "3");
        prop.setProperty("maxActive", "6");
        prop.setProperty("minIdle", "1");
        prop.setProperty("maxWait", "60000");
        prop.setProperty("filters", "stat");
        prop.setProperty("timeBetweenEvictionRunsMillis", "35000");
        prop.setProperty("minEvictableIdleTimeMillis", "300000");//连接的最小空闲时间
        prop.setProperty("testWhileIdle", "true");
        prop.setProperty("testOnBorrow", "false");
        prop.setProperty("testOnReturn", "false");
        prop.setProperty("poolPreparedStatements", "false");
        prop.setProperty("maxPoolPreparedStatementPerConnectionSize", "200");
        prop.setProperty("removeAbandoned", "true");
        return prop;
    }


    /**
     * 判断是否存在
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/12 19:31
     */
    public abstract boolean containsId(String id);

    /**
     * put Map
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/12 19:33
     */
    public abstract void putSourceMap(String sourceMapKey, DruidDataSource druidDataSource);

    /**
     * remove Map
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/12 19:33
     */
    public abstract void removeSourceMap(String sourceMapKey);

    /**
     * 获取sourceMap的key
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/12 19:28
     */
    public static String getSourceMapKey(String ip, int port,String Instances) {
        return ip + "_" + port + "_" + Instances;
    }
}
